#!/bin/bash
#export GOPRIVATE=*.elinkchina.com.cnexport GOPRIVATE=*.elinkchina.com.cn
#安装go
#使用gvm方式安装，可管理版本
installGo() {
    echo "下载gvm"
    G_EXPERIMENTAL="true"
    G_HOME=/home/${user}/dev/gvm
    echo "gvm安装目录：${G_HOME}"
    impGoRoot=$(cat /home/${user}/.bash_path | grep GOROOT | head -n 1)
    if [ -z "${impGoRoot}" ]; then
        echo ""  >>/home/${user}/.bash_path
        echo "export G_EXPERIMENTAL=true" >>/home/${user}/.bash_path #让G_HOME的目录可以作为gvm的根目录，否则会是～/.g目录
        echo "export G_HOME=${G_HOME}" >>/home/${user}/.bash_path
        echo "export G_MIRROR=https://golang.google.cn/dl/" >>/home/${user}/.bash_path

        sed -i "/export GOROOT/d" /home/${user}/.bash_path
        sed -i "/export GOBIN/d" /home/${user}/.bash_path
        echo "export GOROOT=${G_HOME}/go" >>/home/${user}/.bash_path
        echo "export GOBIN=\$GOROOT/bin" >>/home/${user}/.bash_path

        echo "export GOARCH=amd64" >>/home/${user}/.bash_path
        echo "export GOOS=linux" >>/home/${user}/.bash_path
        echo "export GO111MODULE=on" >>/home/${user}/.bash_path
        echo "export GOPROXY=https://goproxy.cn" >>/home/${user}/.bash_path
        #export GOPROXY=https://mirrors.aliyun.com/goproxy/

        echo "export PATH=\$G_HOME:\$GOROOT/bin:\$PATH" >>/home/${user}/.bash_path
        echo "" >>/home/${user}/.bash_path
        source /home/${user}/.bash_path
    fi

    if [ ! -d ${G_HOME} ]; then
        mkdir -p ${G_HOME}
        # cd ${G_HOME}
        # wget http://soft.qiniu.anzhongwei.cn/go${version}.linux-amd64.tar.gz
        wget -P ${G_HOME} https://gitee.com/azhw/gvm/attach_files/676602/download/g-v1.2.0.linux-amd64
        mv ${G_HOME}/g-v1.2.0.linux-amd64 ${G_HOME}/gvm
        chmod +x ${G_HOME}/gvm
    fi

    gvmVersion=$(gvm -v)
    echo "当前gvm版本：$(gvm -v)"

    echo "当前Golang稳定版本："
    gvm ls-remote stable
    echo "当前Golang历史版本："
    remotVersion=$(gvm ls-remote|tr "\n" ","|sed -e 's/,$/\n/')
    echo ${remotVersion}
    read -p "选择要安装的版本 -1则退:" version
    if [ $version == "-1" ]; then
        echo "退出"
        return 0
    fi
    echo $version
    gvm install $version
    gvm use $version
    go version

}


#独立版本安装方式
# installGo() {
#     echo "指定要安装的go版本:"
#     read version
#     GOROOT=/home/${user}/dev/go${version}
#     GOPATH=/home/${user}/project/golang${version}
#     echo ${GOROOT}
#     impGoRoot=$(cat /home/${user}/.bash_path | grep GOROOT | head -n 1)
#     if [ -z "${impGoRoot}" ]; then
#         echo "export GOARCH=amd64" >>/home/${user}/.bash_path
#         echo "export GOOS=linux" >>/home/${user}/.bash_path
#         echo "export GO111MODULE=on" >>/home/${user}/.bash_path
#         echo "export GOPROXY=https://goproxy.io" >>/home/${user}/.bash_path
#         #export GOPROXY=https://mirrors.aliyun.com/goproxy/
#         echo "" >>/home/${user}/.bash_path
        
#     fi

#     if [ ! -d ${GOROOT} ]; then
#         cd /home/${user}/dev
#         # wget http://soft.qiniu.anzhongwei.cn/go${version}.linux-amd64.tar.gz
#         wget https://dl.google.com/go/go${version}.linux-amd64.tar.gz
#         tar -xvf go${version}.linux-amd64.tar.gz
#         mv go go${version}
#     fi
        
#     sed -i "/export GOROOT/d" /home/${user}/.bash_path
#     sed -i "/export GOPATH/d" /home/${user}/.bash_path
#     sed -i "/export GOBIN/d" /home/${user}/.bash_path
#     sed -i "/\$GOPATH\/bin/d" /home/${user}/.bash_path
#     echo "export GOROOT=${GOROOT}" >>/home/${user}/.bash_path
#     echo "export GOPATH=${GOPATH}" >>/home/${user}/.bash_path
#     echo "export GOBIN=\$GOROOT/bin" >>/home/${user}/.bash_path
#     echo "export PATH=\$GOPATH/bin:\$GOROOT/bin:\$PATH" >>/home/${user}/.bash_path
#     source /home/${user}/.bash_path

#     if [ ! -d /home/${user}/project/golang${version} ]; then
#         mkdir -p /home/${user}/project/golang${version}
#         mkdir -p /home/${user}/project/golang${version}/bin
#         mkdir -p /home/${user}/project/golang${version}/src
#         mkdir -p /home/${user}/project/golang${version}/pkg
#     fi
#     /bin/echo -e "当前GO版本号："
#     go version
# }

installGo